﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Text;
using Portal.Web.Global;
using Portal.Web.Framework;
using Portal.Core;
using Portal.AssetTn.Model;


namespace Portal.Gui.Web.WebParts.AssetTn
{
    public class AssetTN_ViewRecord : WebPart
    {
        //public string SessionId = System.Guid.NewGuid().ToString();
        public override string WebPartId
        {
            get
            {
                return "AssetTN.ViewRecord";
            }
        }


        public override WebPartServiceCls WebPartService
        {
            get
            {
                return AssetUtility.WebPartService;
            }
        }

        public override string Title
        {
            get
            {
                return "Xem chi tiết hiện vật";
            }
        }

        public override string Description
        {
            get
            {
                return "";
            }
        }


        public override string RenderWebPart(LoadParamCls loadParamCls)
        {
            try
            {
                AjaxPro.Utility.RegisterTypeForAjax(typeof(AssetTN_ViewRecord));
                string SiteId = loadParamCls.SiteId;
                string AssetId = (string)Environments.Request("AssetId");
                if (string.IsNullOrEmpty(AssetId)) throw new Exception("Tham số truy cập không hợp lệ!");
                string Mode = (string)Environments.Request("Mode");
                string Subject = loadParamCls.RefTitle;
                if (string.IsNullOrEmpty(Subject))
                {
                    Subject = Title;
                }


                bool HasPermission =
                    Permission.CheckPermission(
                       SiteId,
                       Environments.CurrentLoginUser.LoginName,
                       Portal.AssetTn.Security.Permission.GetPermissionId(),
                       Portal.AssetTn.Security.Permission.ePermisionType.Portal_AssetTn_Main.ToString(),
                       Portal.AssetTn.Security.Permission.eFunction.Portal_AssetTn_InputData.ToString(),
                       Portal.AssetTn.Security.Permission.GetPermissionId(),
                       null,
                       false);
                AssetModel oAssetModel = AssetBll.CreateModel(SiteId, AssetId, true);
                ParamCls[] Params = AssetViewFieldBll.GetUpdateParams(SiteId, oAssetModel.frkAssetTypeId);
                string RetHtml =
                          " <script language=javascript>\r\n" +
                          "     function ClientPrint()\r\n" +
                          "     {\r\n" +
                          "         window.open('Go.aspx?wpid=AssetTN.PrintAssetOption&AssetId=" + AssetId + "','_self');\r\n" +
                          "     }\r\n" +

                          "     function ClientUpdateMode()\r\n" +
                          "     {\r\n" +
                          "         SiteId = '" + SiteId + "';\r\n" +
                          "         AssetId=document.getElementById('txtAssetId').value;\r\n" +
                          "         LoadFromBackEnd=document.getElementById('txtLoadFromBackEnd').value;\r\n" +
                          "         Mode=document.getElementById('txtMode').value;\r\n" +
                          "         if(Mode=='view')\r\n" +
                          "         {\r\n" +
                          "             Mode='update';\r\n" +
                          "             document.getElementById('cmdAddImage').style.display='block';\r\n" +
                          "             document.getElementById('cmdAddLog').style.display='block';\r\n" +
                          //"             oAjaxReturn=Portal.Gui.Web.WebParts.AssetTn.AssetTN_ViewRecord.GetUpdateForm(SiteId,AssetId,LoadFromBackEnd).value;\r\n" +
                          //"             document.getElementById('divHolderMainContent').innerHTML=oAjaxReturn.HtmlContent;\r\n" +
                          "             document.getElementById('divViewAsset').style.display='none';\r\n"+
                          "             document.getElementById('divUpdateAsset').style.display='block';\r\n" +
                          "         }\r\n" +
                          "         else\r\n" +
                          "         {\r\n" +
                          "             Mode='view';\r\n" +
                          "             document.getElementById('cmdAddImage').style.display='none';\r\n" +
                          "             document.getElementById('cmdAddLog').style.display='none';\r\n" +
                          "             oAjaxReturn=Portal.Gui.Web.WebParts.AssetTn.AssetTN_ViewRecord.GetViewForm(SiteId,AssetId,LoadFromBackEnd).value;\r\n" +
                          "             document.getElementById('divViewAsset').innerHTML=oAjaxReturn.HtmlContent;\r\n" +
                          "             document.getElementById('divViewAsset').style.display='block';\r\n" +
                          "             document.getElementById('divUpdateAsset').style.display='none';\r\n" +
                          "         }\r\n" +
                          "         document.getElementById('txtMode').value=Mode;\r\n" +
                          "     }\r\n" +

                          "     function ClientCancelSave()\r\n" +
                          "     {\r\n" +
                          "         ClientUpdateMode();\r\n" +
                          "     }\r\n" +

                          "     function ClientSaveInfo()\r\n" +
                          "     {\r\n" +
                          "         SiteId = '" + SiteId + "';\r\n" +
                          "         AssetId=document.getElementById('txtAssetId').value;\r\n" +
                          "         LoadFromBackEnd=document.getElementById('txtLoadFromBackEnd').value;\r\n" +
                          "         oAjaxReturn=Portal.Gui.Web.WebParts.AssetTn.AssetTN_ViewRecord.ServerSideGetAssetObject(SiteId,AssetId).value;\r\n" +
                          "         if(oAjaxReturn.Error)\r\n" +
                          "         {\r\n" +
                          "             alert(oAjaxReturn.InfoMessage);\r\n" +
                          "             return;\r\n" +
                          "         }\r\n" +
                          "         SaveObject=oAjaxReturn.RetObject;\r\n";
                for (int iIndex = 0; iIndex < Params.Length; iIndex++)
                {
                    bool Allow = true;
                    if (Params[iIndex].Key.ToLower().Equals("EntryDate".ToLower()))
                    {
                        Allow = false;
                    }
                    if (Allow)
                    {
                        RetHtml +=
                            "         SaveObject." + Params[iIndex].Key + " =  document.getElementById('txt" + Params[iIndex].Key + "').value;\r\n";
                    }
                }

                RetHtml+= 
                          "         oAjaxReturn=Portal.Gui.Web.WebParts.AssetTn.AssetTN_ViewRecord.ServerSideSaveAsset(SiteId,AssetId,SaveObject).value;\r\n" +
                          "         if(oAjaxReturn.Error)\r\n" +
                          "         {\r\n" +
                          "             alert(oAjaxReturn.InfoMessage);\r\n" +
                          "             return;\r\n" +
                          "         }\r\n" +
                          "         alert(oAjaxReturn.InfoMessage);\r\n" +
                          "         ClientUpdateMode();\r\n" +
                          "     }\r\n" +

                          " function ClientDeleteAsset(AssetId)\r\n" +
                          " {\r\n" +
                          "         SiteId = '" + SiteId + "';\r\n" +
                          "         if(confirm('Bạn đã chắc chắn xóa hiện vật này chưa?')==false)return;\r\n" +
                          "         oAjaxReturn=Portal.Gui.Web.WebParts.AssetTn.AssetTN_ViewRecord.ServerSideDeleteAsset(SiteId,AssetId).value;\r\n" +
                          "         if(oAjaxReturn.Error)\r\n" +
                          "         {\r\n" +
                          "             alert(oAjaxReturn.InfoMessage);\r\n" +
                          "             return;\r\n" +
                          "         }\r\n" +
                          "         alert(oAjaxReturn.InfoMessage);\r\n" +
                          "         window.open(oAjaxReturn.RetUrl,'_self');\r\n" +
                          " }\r\n" +

                          " function ClientDeleteImage(AssetId, AssetListImageId)\r\n" +
                          " {\r\n" +
                          "     SiteId='" + SiteId + "';\r\n" +
                          "     if(confirm('Bạn đã chắc chắn xóa ảnh minh họa chưa?')==false)return;\r\n" +
                          "     AjaxOut = Portal.Gui.Web.WebParts.AssetTn.AssetTN_ViewRecord.ServerSideDeleteImage(SiteId,AssetId, AssetListImageId).value;\r\n" +
                          "     if(AjaxOut.Error)\r\n" +
                          "     {\r\n" +
                          "         alert(AjaxOut.InfoMessage);\r\n" +
                          "         return;\r\n" +
                          "     }\r\n" +
                          "     document.getElementById('divImageContentHolder').innerHTML=AjaxOut.HtmlContent;\r\n" +
                          " }\r\n" +

                          " function ClientSaveUploadImage(AssetId)\r\n" +
                          " {\r\n" +
                          "     SiteId='" + SiteId + "';\r\n" +
                          "     LoadFromBackEnd=document.getElementById('txtLoadFromBackEnd').value;\r\n" +
                          "     AjaxOut = Portal.Gui.Web.WebParts.AssetTn.AssetTN_ViewRecord.ServerSideSaveUploadImage(SiteId,AssetId, LoadFromBackEnd).value;\r\n" +
                          "     if(AjaxOut.Error)\r\n" +
                          "     {\r\n" +
                          "         alert(AjaxOut.InfoMessage);\r\n" +
                          "         return;\r\n" +
                          "     }\r\n" +
                          "     window.open(AjaxOut.RetUrl,'_self');\r\n" +
                          " }\r\n" +

                          " function ClientShowUploadForm()\r\n"+
                          " {\r\n"+
                          "     if(document.getElementById('divAjaxUploadContent').style.display=='none')\r\n"+
                          "     {\r\n"+
                          "         document.getElementById('divAjaxUploadContent').style.display='block';\r\n"+
                          "         document.getElementById('cmdAddImage').value='Hoàn thành';\r\n" +
                          "     }\r\n"+
                          "     else\r\n"+
                          "     {\r\n"+
                          "         document.getElementById('divAjaxUploadContent').style.display='none';\r\n"+
                          "         document.getElementById('cmdAddImage').value='Thêm ảnh minh họa';\r\n" +
                          "     }\r\n"+
                          " }\r\n"+
                          " </script>\r\n" +
                          " <input id=\"txtAssetId\" value=\"" + AssetId + "\" type=\"hidden\">\r\n" +
                          " <input id=\"txtLoadFromBackEnd\" value=\"" + (loadParamCls.LoadFromBackend ? 1 : 0) + "\" type=\"hidden\">\r\n" +
                          " <input id=\"txtMode\" value=\"view\" type=\"hidden\">\r\n" +
                          " <div id=divHolderMainContent>\r\n" +
                          "     <div id=divViewAsset style=\"display:block;\">\r\n" +
                                    GetViewForm(SiteId, AssetId, loadParamCls.LoadFromBackend ? 1 : 0).HtmlContent +
                          "     </div>\r\n"+
                          "     <div id=divUpdateAsset style=\"display:none;\">\r\n"+
                                    GetUpdateForm(SiteId, AssetId, loadParamCls.LoadFromBackend ? 1 : 0).HtmlContent +
                          "     </div>\r\n"+
                          "</div>\r\n" +
                          "<h2>Ảnh minh họa của hiện vật<input id=cmdAddImage type=button class=button style=\"float:right;\" value=\"Thêm ảnh minh họa\" onclick=\"javascript:ClientShowUploadForm();\"></h2>\r\n" +
                           "<div class=cssBlock> \r\n";
                if (HasPermission)
                {
                    RetHtml +=
                          " <div id=divAjaxUploadContent style=\"display:none;\">\r\n" +
                                AjaxUploadServiceBll.GetUploadForm(SiteId, SessionId, "javascript:ClientSaveUploadImage('" + AssetId + "');") +
                          " </div>\r\n";
                }

                string AddRelationButton = "<input id=cmdAddRelation type=button class=button style=\"float:right;display:none;\" value=\"Thêm liên quan\">";
                if (HasPermission == false) AddRelationButton = "";
                RetHtml+=
                          " <div id=\"divImageContentHolder\">\r\n" +
                              ReadingImages(SiteId, AssetId) + 
                          " </div>\r\n" +
                          "</div>\r\n"+

                          "<h2>Hiện vật liên quan"+AddRelationButton+"</h2>\r\n" +
                          "<div class=divAssetHistory>\r\n" +
                            ReadingAssetListRelation(SiteId,AssetId,loadParamCls.LoadFromBackend?1:0)+
                          "</div>\r\n"+

                          "<h2>Lịch sử hiện vật<input id=cmdAddLog type=button class=button style=\"float:right;display:none;\" value=\"Thêm lịch sử\"></h2>\r\n" +
                          "<div class=divAssetHistory>\r\n" +
                                ReadingAssetHistory(SiteId,AssetId)+
                          "</div>\r\n";
                
                if (Mode == null) Mode = "v";
                if (Mode.ToLower().Equals("u"))
                {
                    RetHtml += "<script language=javascript>\r\n" +
                               "     ClientUpdateMode();\r\n"+
                               "</script>\r\n";
                }
                return RetHtml;
            }
            catch (Exception ex)
            {
                return ex.Message.ToString();
            }
        }

        [AjaxPro.AjaxMethod]
        public AjaxReturn ServerSideGetAssetObject(string SiteId, string AssetId)
        {
            AjaxReturn oAjaxReturn = new AjaxReturn();
            try
            {
                AssetModel oAssetModel = AssetBll.CreateModel(SiteId, AssetId, true);
                oAjaxReturn.RetObject = oAssetModel;
            }
            catch (Exception ex)
            {
                oAjaxReturn.Error = true;
                oAjaxReturn.InfoMessage = ex.Message.ToString();
            }
            return oAjaxReturn;
        }

        [AjaxPro.AjaxMethod]
        public AjaxReturn ServerSideSaveAsset(
            string SiteId, 
            string AssetId, 
            AssetModel SaveAssetModel)
        {
            AjaxReturn oAjaxReturn = new AjaxReturn();
            try
            {
                Environments.CheckSessionTimeOut(true);
                AssetBll.SaveAsset(SiteId, AssetId, SaveAssetModel);
                oAjaxReturn.InfoMessage = "Cập nhật hiện vật thành công!";
            }
            catch (Exception ex)
            {
                oAjaxReturn.Error = true;
                oAjaxReturn.InfoMessage = ex.Message.ToString();
            }
            return oAjaxReturn;
        }

        [AjaxPro.AjaxMethod]
        public AjaxReturn ServerSideDeleteAsset(string SiteId, string AssetId)
        {
            AjaxReturn oAjaxReturn = new AjaxReturn();
            try
            {
                Environments.CheckSessionTimeOut(true);
                AssetBll.DeleteAsset(SiteId, AssetId);
                oAjaxReturn.InfoMessage = "Xóa hiện vật thành công!";
                oAjaxReturn.RetUrl = "default.aspx";
            }
            catch (Exception ex)
            {
                oAjaxReturn.Error = true;
                oAjaxReturn.InfoMessage = ex.Message.ToString();
            }
            return oAjaxReturn;
        }


        [AjaxPro.AjaxMethod]
        public AjaxReturn GetViewForm(string SiteId, string AssetId, int LoadFromBackend)
        {
            AjaxReturn oAjaxReturn=new AjaxReturn();
            try
            {
                bool HasPermission =
                   Permission.CheckPermission(
                      SiteId,
                      Environments.CurrentLoginUser.LoginName,
                      Portal.AssetTn.Security.Permission.GetPermissionId(),
                      Portal.AssetTn.Security.Permission.ePermisionType.Portal_AssetTn_Main.ToString(),
                      Portal.AssetTn.Security.Permission.eFunction.Portal_AssetTn_InputData.ToString(),
                      Portal.AssetTn.Security.Permission.GetPermissionId(),
                      null,
                      false);


                AssetModel oAssetModel = AssetBll.CreateModel(SiteId, AssetId, true);

                
                string UpdateButton = "<img onclick=\"javascript:ClientUpdateMode();\" title='Tắt hoặc bật chế độ cập nhật sửa chữa' alt='Tắt hoặc bật chế độ cập nhật sửa chữa' src=\"templates/default/images/system-software-update.png\" style=\"float:right;width:24px;cursor:pointer;\">";
                string DeleteButton = "<img onclick=\"javascript:ClientDeleteAsset('" + AssetId + "');\" title='Xóa hiện vật này' alt='Xóa hiện vật này' src=\"templates/default/images/trash-can-delete.png\" style=\"float:right;width:24px;cursor:pointer;margin-left:8px;\">";
                string PrintButton = "<img onclick=\"javascript:ClientPrint();\" title='In thẻ hiện vật' alt='In thẻ hiện vật' src=\"templates/default/images/print_64.png\" style=\"float:right;width:24px;cursor:pointer;margin-left:8px;\">";
                if (HasPermission == false)
                {
                    UpdateButton = "";
                    DeleteButton = "";
                    PrintButton = "";
                }
                string RetHtml =
                              " <table>\r\n" +
                              " <tr>\r\n" +
                              "     <td valign=top><center><img src=\"templates/default/images/no-image.png\" style=\"width:80px;margin-right:2px;\"></center></td>\r\n" +
                              "     <td valign=top>\r\n" +
                              "         <table class=tabResultInner>\r\n" +
                              "             <tr>\r\n" +
                              "                 <td class=c1>Loại:</td>\r\n" +
                              "                 <td class=c2>" + oAssetModel.frkAssetTypeId + "</td>\r\n" +
                              "             </tr>\r\n"+
                              "             <tr>\r\n" +
                              "                 <td class=c1>Mã phân loại:</td>\r\n" +
                              "                 <td class=c2><img src=\"templates/default/images/" + oAssetModel.Icon + "\" style=\"float:left;width:32px;margin-right:2px;\">" + oAssetModel.ClassCode +PrintButton +DeleteButton+ UpdateButton+"</td>\r\n" +
                              "             </tr>\r\n";
                if (!string.IsNullOrEmpty(oAssetModel.InventoryCode))
                {
                    RetHtml +=
                          "             <tr>\r\n" +
                          "                 <td class=c1>Mã kiểm kê:</td>\r\n" +
                          "                 <td class=c2>" + oAssetModel.InventoryCode + "</td>\r\n" +
                          "             </tr>\r\n";
                }
                if (!string.IsNullOrEmpty(oAssetModel.ProfileCode))
                {
                    RetHtml +=
                          "             <tr>\r\n" +
                          "                 <td class=c1>Mã hồ sơ:</td>\r\n" +
                          "                 <td class=c2>" + oAssetModel.ProfileCode + "</td>\r\n" +
                          "             </tr>\r\n";
                }
                if (!string.IsNullOrEmpty(oAssetModel.CollectCode))
                {
                    RetHtml +=
                          "             <tr>\r\n" +
                          "                 <td class=c1>Số sưu tầm:</td>\r\n" +
                          "                 <td class=c2>" + oAssetModel.CollectCode + "</td>\r\n" +
                          "             </tr>\r\n";
                }
                if (!string.IsNullOrEmpty(oAssetModel.CollectDate))
                {
                    string sCollectDate = oAssetModel.CollectDate;
                    try
                    {
                        DateTime dCollectDate = DateTime.Parse(sCollectDate);
                        sCollectDate = dCollectDate.ToString("dd/MM/yyyy");
                    }
                    catch { sCollectDate = ""; }
                    RetHtml +=
                          "             <tr>\r\n" +
                          "                 <td class=c1>Ngày sưu tầm:</td>\r\n" +
                          "                 <td class=c2>" + sCollectDate + "</td>\r\n" +
                          "             </tr>\r\n";
                }
                RetHtml +=
                          "             <tr>\r\n" +
                          "                 <td class=c1>Tên hiện vật:</td>\r\n" +
                          "                 <td class=c2>" + oAssetModel.Subject + "</td>\r\n" +
                          "             </tr>\r\n";

                if (!string.IsNullOrEmpty(oAssetModel.OtherSubject))
                {
                    RetHtml +=
                          "             <tr>\r\n" +
                          "                 <td class=c1>Tên khác:</td>\r\n" +
                          "                 <td class=c2>" + oAssetModel.OtherSubject + "</td>\r\n" +
                          "             </tr>\r\n";
                }

                if (!string.IsNullOrEmpty(oAssetModel.AssetStatus))
                {
                    RetHtml +=
                          "             <tr>\r\n" +
                          "                 <td class=c1>Trạng thái:</td>\r\n" +
                          "                 <td class=c2>" + oAssetModel.AssetStatus + "</td>\r\n" +
                          "             </tr>\r\n";
                }


                if (!string.IsNullOrEmpty(oAssetModel.Material))
                {
                    RetHtml +=
                          "             <tr>\r\n" +
                          "                 <td class=c1>Chất liệu:</td>\r\n" +
                          "                 <td class=c2>" + oAssetModel.Material + "</td>\r\n" +
                          "             </tr>\r\n";
                }

                if (!string.IsNullOrEmpty(oAssetModel.RegDate))
                {
                    string sCollectDate = oAssetModel.CollectDate;
                    try
                    {
                        DateTime dCollectDate = DateTime.Parse(sCollectDate);
                        sCollectDate = dCollectDate.ToString("dd/MM/yyyy");
                    }
                    catch { sCollectDate = ""; }

                    RetHtml +=
                              "             <tr>\r\n" +
                              "                 <td class=c1>Ngày đăng ký:</td>\r\n" +
                              "                 <td class=c2>" + sCollectDate + "</td>\r\n" +
                              "             </tr>\r\n";
                }

                if (!string.IsNullOrEmpty(oAssetModel.InputDate))
                {
                    string sInputDate = oAssetModel.InputDate;
                    try
                    {
                        DateTime dInputDate = DateTime.Parse(sInputDate);
                        sInputDate = dInputDate.ToString("dd/MM/yyyy");
                    }
                    catch { sInputDate = ""; }

                    RetHtml +=
                              "             <tr>\r\n" +
                              "                 <td class=c1>Ngày vào sổ:</td>\r\n" +
                              "                 <td class=c2>" + sInputDate + "</td>\r\n" +
                              "             </tr>\r\n";
                }

                if (!string.IsNullOrEmpty(oAssetModel.Characteristic))
                {
                    RetHtml +=
                          "             <tr>\r\n" +
                          "                 <td class=c1>Đặc thù:</td>\r\n" +
                          "                 <td class=c2>" + oAssetModel.Characteristic + "</td>\r\n" +
                          "             </tr>\r\n";
                }
                if (!string.IsNullOrEmpty(oAssetModel.Size))
                {
                    RetHtml +=
                          "             <tr>\r\n" +
                          "                 <td class=c1>Kích cỡ:</td>\r\n" +
                          "                 <td class=c2>" + oAssetModel.Size + "</td>\r\n" +
                          "             </tr>\r\n";
                }
                if (!string.IsNullOrEmpty(oAssetModel.TechnicalMade))
                {
                    RetHtml +=
                          "             <tr>\r\n" +
                          "                 <td class=c1>Kỹ thuật chế tác:</td>\r\n" +
                          "                 <td class=c2>" + oAssetModel.TechnicalMade + "</td>\r\n" +
                          "             </tr>\r\n";
                }

                if (!string.IsNullOrEmpty(oAssetModel.FicDescription))
                {
                    RetHtml +=
                          "             <tr>\r\n" +
                          "                 <td class=c1>Mô tả phích:</td>\r\n" +
                          "                 <td class=c2>" + oAssetModel.FicDescription + "</td>\r\n" +
                          "             </tr>\r\n";
                }

                if (!string.IsNullOrEmpty(oAssetModel.NationalName))
                {
                    RetHtml +=
                              "             <tr>\r\n" +
                              "                 <td class=c1>Dân tộc:</td>\r\n" +
                              "                 <td class=c2>" + oAssetModel.NationalName + "</td>\r\n" +
                              "             </tr>\r\n";
                }

                if (!string.IsNullOrEmpty(oAssetModel.MadeBy))
                {
                    string[] Authors = oAssetModel.MadeBy.Split(new char[] { ';', ',' }, StringSplitOptions.RemoveEmptyEntries);
                    string MadeBy = "<ul class=Author>";
                    for (int iIndex = 0; iIndex < Authors.Length; iIndex++)
                    {
                        string ItemUrl = "default.aspx?k=" + Authors[iIndex];
                        if (LoadFromBackend==1)
                        {
                            ItemUrl = Screen.BuildUrl(SiteId, new AssetTN_SimpleSearch().WebPartId,
                                new ParamCls[] 
                                {
                                    new ParamCls("k",Authors[iIndex])
                                });
                        }
                        MadeBy = MadeBy + "<li><a target=\"_self\" href=\"" + ItemUrl + "\">" + Authors[iIndex] + "</a></li>\r\n";
                    }
                    MadeBy += "</ul>\r\n";
                    RetHtml +=
                              "             <tr>\r\n" +
                              "                 <td class=c1>Người tạo:</td>\r\n" +
                              "                 <td class=c2>" + MadeBy + "</td>\r\n" +
                              "             </tr>\r\n";
                }
                if (!string.IsNullOrEmpty(oAssetModel.DeliverBy))
                {
                    RetHtml +=
                           "             <tr>\r\n" +
                          "                 <td class=c1>Người giao:</td>\r\n" +
                          "                 <td class=c2>" + oAssetModel.DeliverBy + "</td>\r\n" +
                          "             </tr>\r\n";
                }
                if (!string.IsNullOrEmpty(oAssetModel.UseFor))
                {
                    RetHtml +=
                    "             <tr>\r\n" +
                    "                 <td class=c1>Mục đích sử dụng:</td>\r\n" +
                    "                 <td class=c2>" + oAssetModel.UseFor + "</td>\r\n" +
                    "             </tr>\r\n";
                }
                if (!string.IsNullOrEmpty(oAssetModel.Quantity))
                {
                    RetHtml +=
                              "             <tr>\r\n" +
                              "                 <td class=c1>Số lượng:</td>\r\n" +
                              "                 <td class=c2>" + oAssetModel.Quantity + "</td>\r\n" +
                              "             </tr>\r\n";
                }
                if (!string.IsNullOrEmpty(oAssetModel.Cost))
                {
                    string CostText = "-";
                    try
                    {
                        CostText = (decimal.Parse(oAssetModel.Cost)).ToString("#,##0");
                    }
                    catch { }
                    RetHtml +=
                              "             <tr>\r\n" +
                              "                 <td class=c1>Giá trị:</td>\r\n" +
                              "                 <td class=c2>" + CostText + "</td>\r\n" +
                              "             </tr>\r\n";
                }

                if (!string.IsNullOrEmpty(oAssetModel.ContentHistory))
                {
                    RetHtml +=
                          "             <tr>\r\n" +
                          "                 <td class=c1>Lịch sử:</td>\r\n" +
                          "                 <td class=c2>" + oAssetModel.ContentHistory + "</td>\r\n" +
                          "             </tr>\r\n";
                }
                if (!string.IsNullOrEmpty(oAssetModel.Description))
                {
                    RetHtml +=
                          "             <tr>\r\n" +
                          "                 <td class=c1>Mô tả:</td>\r\n" +
                          "                 <td class=c2>" + oAssetModel.Description + "</td>\r\n" +
                          "             </tr>\r\n";
                }
                if (!string.IsNullOrEmpty(oAssetModel.Report))
                {
                    RetHtml +=
                          "             <tr>\r\n" +
                          "                 <td class=c1>Biên bản:</td>\r\n" +
                          "                 <td class=c2>" + oAssetModel.Report + "</td>\r\n" +
                          "             </tr>\r\n";
                }
                if (!string.IsNullOrEmpty(oAssetModel.ReceiveBy))
                {
                    RetHtml +=
                              "             <tr>\r\n" +
                              "                 <td class=c1>Người nhận:</td>\r\n" +
                              "                 <td class=c2>" + oAssetModel.ReceiveBy + "</td>\r\n" +
                              "             </tr>\r\n";
                }
                if (!string.IsNullOrEmpty(oAssetModel.Note))
                {
                    RetHtml +=
                              "             <tr>\r\n" +
                              "                 <td class=c1>Ghi chú:</td>\r\n" +
                              "                 <td class=c2>" + oAssetModel.Note + "</td>\r\n" +
                              "             </tr>\r\n";
                }


                if (!string.IsNullOrEmpty(oAssetModel.StoragePlace))
                {
                    RetHtml +=
                              "             <tr>\r\n" +
                              "                 <td class=c1>Nơi lưu trữ:</td>\r\n" +
                              "                 <td class=c2>" + oAssetModel.StoragePlace + "</td>\r\n" +
                              "             </tr>\r\n";
                }


                if (!string.IsNullOrEmpty(oAssetModel.MadeDate))
                {
                    string sMadeDate = oAssetModel.MadeDate;
                    try
                    {
                        DateTime dMadeDate = DateTime.Parse(sMadeDate);
                        sMadeDate = dMadeDate.ToString("dd/MM/yyyy");
                    }
                    catch { sMadeDate = ""; }

                    RetHtml +=
                              "             <tr>\r\n" +
                              "                 <td class=c1>Ngày hiện vật:</td>\r\n" +
                              "                 <td class=c2>" + sMadeDate + "</td>\r\n" +
                              "             </tr>\r\n";
                }

                if (!string.IsNullOrEmpty(oAssetModel.MadePlace))
                {
                    RetHtml +=
                              "             <tr>\r\n" +
                              "                 <td class=c1>Nơi tạo:</td>\r\n" +
                              "                 <td class=c2>" + oAssetModel.MadePlace + "</td>\r\n" +
                              "             </tr>\r\n";
                }
              
                RetHtml +=
                          "         </table>\r\n" +
                          "     </td>\r\n" +
                          "  </tr>\r\n" +
                          "</table>\r\n";
                oAjaxReturn.HtmlContent = RetHtml;
            }
            catch (Exception ex)
            {
                oAjaxReturn.Error = true;
                oAjaxReturn.InfoMessage = ex.Message.ToString();
            }
            return oAjaxReturn;
        }



        [AjaxPro.AjaxMethod]
        public AjaxReturn GetUpdateForm(string SiteId, string AssetId, int LoadFromBackend)
        {
            AjaxReturn oAjaxReturn = new AjaxReturn();
            try
            {
                AssetModel oAssetModel = AssetBll.CreateModel(SiteId, AssetId, true);
                ParamCls[] Params = AssetViewFieldBll.GetUpdateParams(SiteId, oAssetModel.frkAssetTypeId);
                string RetHtml =
                              " <table>\r\n" +
                              " <tr>\r\n" +
                              "     <td valign=top>\r\n" +
                              "         <table class=tabResultInner>\r\n";
                for (int iIndex = 0; iIndex < Params.Length; iIndex++)
                {
                    bool Allow = true;
                    if (Params[iIndex].Key.ToLower().Equals("EntryDate".ToLower()))
                    {
                        Allow = false;
                    }
                    if (Allow)
                    {
                        string Class = "textboxshort";
                        string SaveText = "<img onclick=\"javascript:ClientSaveInfo();\" title='Ghi lại thay đổi' alt='Ghi lại thay đổi' src=\"templates/default/images/save.png\" style=\"float:right;width:24px;cursor:pointer;margin-left:8px;\"> <img onclick=\"javascript:ClientCancelSave();\" title='Hủy không khi quay lại' alt='Hủy không khi quay lại' src=\"templates/default/images/CancelSave.png\" style=\"float:right;width:24px;cursor:pointer;\">";
                        if (iIndex > 0)
                        {
                            Class = "textbox";
                            SaveText = "";
                        }

                        string LookUpText = AssetUtility.GetLookUp(SiteId, Params[iIndex].Key, "txt", AssetRenderSearchBll.GetValue(oAssetModel, Params[iIndex].Key));
                        string InputText = LookUpText;
                        if (string.IsNullOrEmpty(InputText))
                        {
                            InputText = "<input type=textbox class=" + Class + " id=\"txt" + Params[iIndex].Key + "\"  value=\"" + AssetRenderSearchBll.GetValue(oAssetModel, Params[iIndex].Key) + "\">";
                            if (Params[iIndex].Type.Equals("textarea"))
                            {
                                InputText =
                                "<textarea class=textarea type=textarea id=\"txt" + Params[iIndex].Key + "\">" + AssetRenderSearchBll.GetValue(oAssetModel, Params[iIndex].Key) + "</textarea>";
                            }
                            if (Params[iIndex].Type.ToLower().Equals("DateTime".ToLower()))
                            {
                                string dValueDate=AssetRenderSearchBll.GetValue(oAssetModel, Params[iIndex].Key);
                                try
                                {
                                    DateTime dDate = DateTime.Parse(dValueDate);
                                    dValueDate = dDate.ToString("dd/MM/yyyy");
                                }
                                catch { dValueDate = ""; }
                                InputText =
                                    LookUpUtilities.GenerateLookUpDate("txt" + Params[iIndex].Key, dValueDate) + "\r\n";
                            }
                        }
                        RetHtml +=
                                  "             <tr>\r\n" +
                                  "                 <td class=c1>" + Params[iIndex].Title + ":</td>\r\n" +
                                  "                 <td class=c2>" + InputText + SaveText + "</td>\r\n" +
                                  "             </tr>\r\n";
                    }
                }
                RetHtml+=
                              "         </table>\r\n" +
                              "     </td>\r\n" +
                              "  </tr>\r\n" +
                              "</table>\r\n";
                oAjaxReturn.HtmlContent = RetHtml;
            }
            catch (Exception ex)
            {
                oAjaxReturn.Error = true;
                oAjaxReturn.InfoMessage = ex.Message.ToString();
                oAjaxReturn.HtmlContent = ex.Message.ToString();
            }
            return oAjaxReturn;
        }


        string ReadingImages(string SiteId, string AssetId)
        {
            SiteParam oSiteParam = Environments.CreateSiteParam(SiteId);
            AssetImageCls[] AssetImages = AssetBll.ReadingAssetImage(SiteId, AssetId);
            if (AssetImages.Length==0)
            {
                return "<div class=CssNoData>Chưa có hình minh họa cho hiện vật</div>\r\n";
            }

            bool HasPermission =
                 Permission.CheckPermission(
                    SiteId,
                    Environments.CurrentLoginUser.LoginName,
                    Portal.AssetTn.Security.Permission.GetPermissionId(),
                    Portal.AssetTn.Security.Permission.ePermisionType.Portal_AssetTn_Main.ToString(),
                    Portal.AssetTn.Security.Permission.eFunction.Portal_AssetTn_InputData.ToString(),
                    Portal.AssetTn.Security.Permission.GetPermissionId(),
                    null,
                    false);

            string RetHtml =
                " <ul class=AssetImages>\r\n";
            for (int iIndex = 0; iIndex < AssetImages.Length; iIndex++)
            {
                string AssetListImageId = AssetImages[iIndex].AssetListImageId;
                string ImageFile = AssetImages[iIndex].ImageFile;
                string Ext = AssetImages[iIndex].Ext;
                string Url = oSiteParam.MediaHttpRoot + "/asset.images/" + AssetListImageId + Ext;
                string DeleteButton = "<a href=\"javascript:ClientDeleteImage('" + AssetId + "','" + AssetListImageId + "');\">Xóa</a>";
                if (HasPermission == false)
                {
                    DeleteButton = "";
                }
                RetHtml +=
                    "<li>\r\n" +
                    "   <table>\r\n" +
                    "       <tr>\r\n" +
                    "           <td><img class=image src=\"" + Url + "\"></td>\r\n" +
                    "       <tr>\r\n"+
                    "       <tr>\r\n"+
                    "           <td style=\"text-align:center;\">"+DeleteButton+"</td>\r\n" +
                    "       </tr>\r\n" +
                    "   </table>\r\n"+
                    "</li>\r\n";
            }
            RetHtml +=
                      " </ul>\r\n";
            return RetHtml;
        }

        string ReadingAssetListRelation(string SiteId, string AssetId, int LoadFromBackEnd)
        {
            AssetListRelationCls[] AssetListRelations = AssetBll.ReadingAssetListRelation(SiteId, AssetId);
            if (AssetListRelations.Length == 0)
            {
                return "<div class=CssNoData>Chưa có hiện vật liên quan</div>\r\n";
            }

            bool HasPermission =
               Permission.CheckPermission(
                  SiteId,
                  Environments.CurrentLoginUser.LoginName,
                  Portal.AssetTn.Security.Permission.GetPermissionId(),
                  Portal.AssetTn.Security.Permission.ePermisionType.Portal_AssetTn_Main.ToString(),
                  Portal.AssetTn.Security.Permission.eFunction.Portal_AssetTn_InputData.ToString(),
                  Portal.AssetTn.Security.Permission.GetPermissionId(),
                  null,
                  false);
            string Html=
                          "<table>\r\n"+
                          "     <tr>\r\n" +
                          "         <th>#</th>\r\n" +
                          "         <th width=100>Phân loại</th>\r\n" +
                          "         <th>Tên hiện vật</th>\r\n" +
                          "     </tr>\r\n";
            for (int iIndex = 0; iIndex < AssetListRelations.Length; iIndex++)
            {
                string RelItemAssetListId = AssetListRelations[iIndex].frkRelAssetListId;
                string ClassCode = AssetListRelations[iIndex].ClassCode;
                string Subject = AssetListRelations[iIndex].Subject;

                string Url = AssetBll.GetAssetListUrl(SiteId, RelItemAssetListId, LoadFromBackEnd);
                Html+=
                          "     <tr>\r\n" +
                          "         <td valign=top>"+(iIndex+1)+"</td>\r\n" +
                          "         <td valign=top>"+ClassCode+"</td>\r\n" +
                          "         <td valign=top><a href=\""+Url+"\">"+Subject+"</a></td>\r\n" +
                          "     </tr>\r\n";
            }
            Html +=
                          "</table>\r\n";
            return Html;
        }

        string ReadingAssetHistory(string SiteId, string AssetId)
        {
            AssetHistoryCls[] AssetHistories = AssetBll.ReadingAssetHistory(SiteId, AssetId);
            if (AssetHistories.Length==0)
            {
                return "<div class=CssNoData>Chưa có lịch sử liên quan</div>\r\n";
            }

            bool HasPermission =
                  Permission.CheckPermission(
                     SiteId,
                     Environments.CurrentLoginUser.LoginName,
                     Portal.AssetTn.Security.Permission.GetPermissionId(),
                     Portal.AssetTn.Security.Permission.ePermisionType.Portal_AssetTn_Main.ToString(),
                     Portal.AssetTn.Security.Permission.eFunction.Portal_AssetTn_InputData.ToString(),
                     Portal.AssetTn.Security.Permission.GetPermissionId(),
                     null,
                     false);
            string Html =
                "<table>\r\n" +
                "     <tr>\r\n" +
                "         <th>#</th>\r\n" +
                "         <th>Ngày</th>\r\n" +
                "         <th>Lịch sử</th>\r\n" +
                "         <th>Người</th>\r\n" +
                "     </tr>\r\n";
            for (int iIndex = 0; iIndex < AssetHistories.Length; iIndex++)
            {
                DateTime ActionDate = AssetHistories[iIndex].ActionDate;
                string Subject = AssetHistories[iIndex].Subject;
                string Owner = AssetHistories[iIndex].Owner;
                Html +=
                "     <tr>\r\n" +
                "         <td>"+(iIndex+1)+"</td>\r\n" +
                "         <td>" + ActionDate.ToString("dd/MM/yyyy") + "</td>\r\n" +
                "         <td>"+Subject+"</td>\r\n" +
                "         <td>"+Owner+"</td>\r\n" +
                "     </tr>\r\n";
            }
            Html+=
                "</table>\r\n";
            return Html;
        }

        [AjaxPro.AjaxMethod]
        public AjaxReturn ServerSideSaveUploadImage(string SiteId, string AssetId, string LoadFromBackEnd)
        {
            AjaxReturn oAjaxReturn = new AjaxReturn();
            try
            {
                string SavePath = AjaxUploadServiceBll.GetDirSave(SiteId, AssetId.Substring(0, 8));
                string[] Savefiles = System.IO.Directory.GetFiles(SavePath);
                if (Savefiles.Length == 0) throw new Exception("Chưa upload ảnh minh họa!");
                AssetBll.AddAssetImages(SiteId, AssetId,  Savefiles);
                oAjaxReturn.RetUrl = AssetBll.GetAssetListUrl(SiteId, AssetId, int.Parse(LoadFromBackEnd));
                try
                {
                    System.IO.Directory.Delete(SavePath, true);
                }
                catch { }
            }
            catch (Exception ex)
            {
                oAjaxReturn.Error = true;
                oAjaxReturn.InfoMessage = ex.Message.ToString();
            }
            return oAjaxReturn;
        }


        [AjaxPro.AjaxMethod]
        public AjaxReturn ServerSideDeleteImage(string SiteId,string AssetId, string AssetListImageId)
        {
            AjaxReturn oAjaxReturn = new AjaxReturn();
            try
            {
                AssetBll.DeleteAssetImage(SiteId, AssetListImageId);
                oAjaxReturn.InfoMessage = "Xóa ảnh minh họa thành công!";
                oAjaxReturn.HtmlContent = ReadingImages(SiteId, AssetId);
            }
            catch (Exception ex)
            {
                oAjaxReturn.Error = true;
                oAjaxReturn.InfoMessage = ex.Message.ToString();
            }
            return oAjaxReturn;
        }



        [AjaxPro.AjaxMethod]
        public AjaxReturn ServerSidePrint(string SiteId, string SessionId)
        {
            AjaxReturn oAjaxReturn = new AjaxReturn();
            try
            {
                //doan nay tim kiem
                Environments.CheckSessionTimeOut(true);
                //DataSet dsResult = null;
                //AssetModel[] AssetModels = AssetBll.Search(SiteId, "sobaoquantamthoi", null, "EntryDate", ref dsResult);
                //if (AssetModels.Length == 0) throw new Exception("Không có dữ liệu để in sổ!");
                //dsResult.Tables[0].TableName = "TableAssetList";
                //dsResult.DataSetName = "TableAssetList";
                //ReportServiceBll.SaveData(SiteId, Environments.CurrentLoginUser.LoginName, dsResult, SessionId);
                //oAjaxReturn.RetUrl = "PrintTemporary.aspx?SessionId=" + SessionId;

            }
            catch (Exception ex)
            {
                oAjaxReturn.Error = true;
                oAjaxReturn.InfoMessage = ex.Message.ToString();
            }
            return oAjaxReturn;
        }

        //[AjaxPro.AjaxMethod]
        //public DateTime ServerSideConvertToDate(string VNDate)
        //{
        //    if (string.IsNullOrEmpty(VNDate))
        //    {
        //        return new System.DateTime();
        //    }
        //    else
        //    {
        //        return Portal.Utilities.FuncUtilities.VNDateToDate(VNDate);
        //    }
        //}

        public override void RegAjaxPro(System.Web.UI.Page ActivePage)
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof(AssetTN_ViewRecord), ActivePage);
        }
    }
}
